addpath(genpath('C:\Users\custs\workspace\matlab\congcong\lib\eeglab2023'));
addpath(genpath('C:\Users\custs\workspace\matlab\congcong\lib\fieldtrip-master'));
%%存储每个脑电数据不同的event

%% read EEG data, separately for each condition
eeglab nogui;
clear; close all;
cd('C:\Users\custs\workspace\matlab\congcong\data\aMCI')  %aMCI  % HC
id={'103','106','204'};

Ns = length(id);

for subi=1:Ns
    LoadName = [ id{subi} '.set'];               % 定义要读入数据的名字

    % load continuous resting EEG data
    EEG = pop_loadset('filename',LoadName);

    %%% select condition
    EEG1 = pop_selectevent(EEG, 'type',[1111], 'deleteevents','on','deleteepochs','on','invertepochs','off'); % select condition
    EEG2 = pop_selectevent(EEG, 'type',[2222], 'deleteevents','on','deleteepochs','on','invertepochs','off'); % select condition


    %% 在type 字段 后 可以添加如：,rt‘，  ’500<=1500‘，  ’acc',1,
    % convert into fieldtrip format
    condA = eeglab2fieldtrip(EEG1, 'raw');
    condB = eeglab2fieldtrip(EEG2, 'raw');


    cfg = [];
    condA = ft_preprocessing(cfg, condA);
    condB = ft_preprocessing(cfg, condB);
    save(['C:\Users\custs\workspace\matlab\congcong\data\aMCI\clean\' id{subi} '_amciCleanData.mat'], 'condA', 'condB')
    % save(['C:\Users\liu\Desktop\脑电研究\时频分析程序\readfiled\' id{subi} '_CleanData.mat'], 'condA', 'condB')
    % 保存数据到另一个文件夹,使用绝对路径

end





% %% read EEG data, separately for each condition  save single set
% eeglab nogui;
% clear; close all;
% cd('C:\Users\liu\Desktop\脑电研究\海宁学生数据\波形改marker后\aMCI组\')
% id = {'110','114' ,'119','120','121','122','124','202','203','205','208','209','213','217','218','220','221','223','224','225','227','230','231','232','233', '235','236','238','240','241','242'};  % amci 被试编号
% % id={'101','102','104','105','107','108','109','111','112','113','115','118','123','125','127','128','207','210','211','212','214','216','219','222','226','228','229','234','237','239','243'} ; %hc
%
% Ns = length(id);
%
% for subi=1:Ns
%     LoadName = [ id{subi} '.set'];               % 定义要读入数据的名字
%
%     % load continuous resting EEG data
%     EEG = pop_loadset('filename',LoadName);
%
%     %%% select condition
%     EEG1 = pop_selectevent(EEG, 'type',[1010], 'deleteevents','on','deleteepochs','on','invertepochs','off'); % select condition
%     EEG2 = pop_selectevent(EEG, 'type',[2010], 'deleteevents','on','deleteepochs','on','invertepochs','off'); % select condition
%
%      pop_saveset(EEG1,['C:\Users\liu\Desktop\脑电研究\脑网络\新处理学生数据\amci-go\' id{subi} '_go.set']);
%
%       pop_saveset(EEG2,['C:\Users\liu\Desktop\脑电研究\脑网络\新处理学生数据\amci-nogo\' id{subi} '_nogo.set']);
%
%     % 保存数据到另一个文件夹,使用绝对路径
%
% end


%% 旧方法，不推荐使用
% cfg = [];
% cfg.dataset                 = LoadName;                   % 数据的名称
% cfg.channel                 = 'all';                    % 要读入的电极，默认是所有电极
% cfg.trialdef.eventtype      = '?';
% % 如果不知道eventtype，可以先将这一项设置为 '?'，然后运行到 cfg = ft_definetrial(cfg)，程序会输出eventtype
% cfg.trialdef.prestim        = 1;                     % epoch 开始的时间，以秒为单位
% cfg.trialdef.poststim       = 1.498;                 % epoch 结束的时间，应比原来epoch结束的时间短一个采样点
% cfg.trialdef.eventvalue     = {'11'};      % 要读入的条件所对应的marker,可以多个,eg,{'11','12'}
% % 不同的厂商的marker变量类型，有的是字符型；有的是数值型。如是数值型，则输入{11, 12}
% cfg = ft_definetrial(cfg);
% condA = ft_preprocessing(cfg);                          % 读取该条件的数据，并对得到的变量命名
%
